﻿using ITSM.Model;
using ITSM.Model.Entity;
using ITSM.Model.Model;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ITSM.Service
{
    public class DemandTypeLink : BaseDB, IDemandTypeLink
    {
        private readonly ILogger _logger;

        public DemandTypeLink(ILogger<DemandTypeLink> logger)
        {
            _logger = logger;
        }
        public List<RYD_DemandTypeLink> GetDemandTypeLink(string RYD_Company, string[] RYD_DemandTypeNo)
        {
            if (RYD_DemandTypeNo.Length == 0)
            {
                return db.Queryable<RYD_DemandTypeLink, RYD_UserInfo>((res, us) => res.RYD_UserId == us.RYD_Id)
                    .Where((res, us) =>
                        (res.RYD_Company == RYD_Company||us.AcrossCompanies==1)
                        && (us.RYD_Company == RYD_Company || us.AcrossCompanies == 1)
                        && us.RYD_IsDelete == 1
                        && (us.RYD_State == 2 || (us.RYD_State == 4 && !string.IsNullOrWhiteSpace(us.RYD_Name)))
                    ).Select((res, us)=>res).ToList();

            }
            List<RYD_DemandTypeLink> typeLinks = db.Queryable<RYD_DemandTypeLink, RYD_UserInfo>((res, us) => res.RYD_UserId == us.RYD_Id)
                .Where((res, us) => res.RYD_Company == RYD_Company && (us.RYD_Company == RYD_Company || us.AcrossCompanies == 1)
                    && us.RYD_IsDelete == 1
                    && us.RYD_State == 2
                    && RYD_DemandTypeNo.Contains(res.RYD_DemandTypeNo + "")
                ).Select((res,us)=>res).ToList();
            return typeLinks;
            throw new NotImplementedException();
        }

        public List<RYD_DemandTypeLink> GetUserTypeLink(RYD_DemandTypeLink typeLink)
        {
            List<RYD_DemandTypeLink> typeLinks = db.Queryable<RYD_DemandTypeLink>().Where(res => res.RYD_Company == typeLink.RYD_Company
            && res.RYD_UserId == typeLink.RYD_UserId).ToList();
            return typeLinks;
            throw new NotImplementedException();
        }

        public bool SetDemandTypeLink(string RYD_UserId, string RYD_Company, int[] RYD_DemandTypeNo)
        {
            db.BeginTran();
            try
            {
                db.Deleteable<RYD_DemandTypeLink>().Where(res => res.RYD_UserId == RYD_UserId && res.RYD_Company == RYD_Company).ExecuteCommand();
                List<RYD_DemandTypeLink> rYD_DemandTypeLinks = new();
                foreach (var item in RYD_DemandTypeNo)
                {
                    RYD_DemandTypeLink rYD_DemandTypeLink = new()
                    {
                        RYD_UserId = RYD_UserId,
                        RYD_Company = RYD_Company,
                        RYD_DemandTypeNo = item,
                        RYD_CreateBy = RYD_UserId,
                        RYD_CreateTime = DateTime.Now,
                        RYD_ModifyBy = RYD_UserId,
                        RYD_ModifyTime = DateTime.Now
                    };
                    rYD_DemandTypeLinks.Add(rYD_DemandTypeLink);
                }
                int res = db.Insertable(rYD_DemandTypeLinks).ExecuteCommand();
                if (res <= 0)
                {
                    db.RollbackTran();
                    _logger.LogError("删除错误");
                    return false;
                }

                db.CommitTran();
                return true;
            }
            catch (Exception)
            {
                db.RollbackTran();
                _logger.LogError("删除错误");
                return false;
            }
        }

        public bool DeleteDemandTypeLink(RYD_RegIt demandTypeLink)
        {
            return db.Deleteable<RYD_DemandTypeLink>().Where(res => res.RYD_UserId == demandTypeLink.RYD_UserId && res.RYD_Company == demandTypeLink.RYD_Company && demandTypeLink.RYD_DemandTypeNo.Contains(res.RYD_DemandTypeNo)).ExecuteCommand() > 0;
            throw new NotImplementedException();
        }

        public bool AddDemandTypeLink(string RYD_UserId, string RYD_Company, int[] RYD_DemandTypeNo)
        {
            List<RYD_DemandTypeLink> typeLinks = new();
            foreach (var item in RYD_DemandTypeNo)
            {
                RYD_DemandTypeLink typeLink = new()
                {
                    RYD_UserId = RYD_UserId,
                    RYD_Company = RYD_Company,
                    RYD_DemandTypeNo = item,
                    RYD_CreateBy = RYD_UserId,
                    RYD_CreateTime = DateTime.Now,
                    RYD_ModifyBy = RYD_UserId,
                    RYD_ModifyTime = DateTime.Now
                };
                typeLinks.Add(typeLink);
            }
            return db.Insertable(typeLinks).ExecuteCommand() > 0;
            throw new NotImplementedException();
        }
    }
}
